<template>
	<view class="register">
	
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="tel"
					type="text"
					maxlength="11"
					placeholder="手机号"
					@blur="toPhone()"
				></wInput>
				<wInput
					v-model="password"
					type="password"
					maxlength="11"
					placeholder="登录密码"
					isShowPass
				></wInput>
				<wInput
					v-model="code"
					type="number"
					maxlength="6"
					placeholder="验证码"
					isShowCode
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
					
			</view>
				
			<wButton 
				class="wbutton"
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					class="cuIcon"
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				> 同意</text>
				<!-- 协议地址 -->
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../../components/watch-login/watch-input.vue' //input
	import wButton from '../../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				registerUrl:{
					sendCodeUrl:'/users/userInfo/sendCode',
					registerUrl:'/users/userInfo/register',
					toPhoneUrl:'/users/userInfo/toPhone',
				},
				//logo图片 base64
				logoImage: '',
				tel:'', // 用户/电话
				password:'', //密码
				code:"", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
				type:0,
				phoneType:true,
			}
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
		},
		methods: {
			/* 验证手机号是否已经注册 */
			toPhone(){
				this.$axios.get(this.registerUrl.toPhoneUrl+"?userPhone="+this.tel).then((res)=>{
					if(!res.data.success){
						this.phoneType=false;
						uni.showToast({
						    icon: 'none',
							position: 'bottom',
						    title: '该手机号已注册'
						});
						return false;
					}else{
						this.phoneType=true;
					}
				})
			},
			isShowAgree(){
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode(){
				//获取验证码
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				if (!reg.test(_this.tel)) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				/* 验证码请求 */
				this.$axios.get(this.registerUrl.sendCodeUrl+"/"+this.tel+"/"+this.type).then((res)=>{
					if(res.data.success){
						uni.showToast({
							icon: 'success',
							position: 'bottom',
							title: '发送成功'
						});
					}else{
						uni.showToast({
							icon: 'error',
							position: 'bottom',
							title: res.data.message
						});
					}
				})
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				setTimeout(function(){
					_this.$refs.runCode.$emit('runCode',0); //假装模拟下需要 终止倒计时
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '验证码已过期'
					});
				},60000)
			},
		    startReg() {
				if(!this.phoneType){
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: "该手机号已被注册"
					});
					return false
				}
				//注册
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《协议》'
				    });
				    return false;
				}
				if (this.tel.length !=11) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
		        if (this.password.length < 6) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return false;
		        }
				if (this.code.length != 6) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				}
				this.$axios.get(this.registerUrl.registerUrl+"/"+this.tel+"/"+this.code+"/"+this.password).then((res)=>{
					if(res.data.success){
						/* 加载中 */
						_this.isRotate=true
						setTimeout(function(){
							_this.isRotate=false
						},1000)
						uni.showToast({
							icon: 'success',
							position: 'bottom',
							title: '注册成功'
						});
						uni.reLaunch({
							url: './login?phoneData='+this.tel+"&passData="+this.password,
						});
					}else{
						uni.showToast({
							icon: 'error',
							position: 'bottom',
							title: res.data.result.message
						});
					}
				})
		    }
		}
	}
</script>

<style>
	@import url("../../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>